import axios from "axios"
import qs from "querystring"
import {
    Toast
} from "antd-mobile"

//请求拦截
axios.interceptors.request.use(config=>{
    return config
})

//响应拦截
axios.interceptors.response.use(res=>{
    //统一打印
    console.group("本次请求地址：" + res.config.url)
    console.log(res);
    console.groupEnd()

    //统一失败处理
    if (res.data.code !== 200) {
        Toast.fail(res.data.msg, 1);
    }

    //返回组件
    return res
})
 
export function get(url,params={}){
    return axios({
        url,
        params
    })
}

export function post(url,params={},isFile=false){
    let data=null;
    if(isFile){
        data=new FormData()
        Object.keys(params).forEach(key=>{
            data.append(key,params[key])
        })
    }else{
        data=qs.stringify(params)
    }
    return axios({
        url,
        data,
        method:"post"
    })
}